/**
 * Copyright (c) 2010 by Kevin Bierhoff.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *    1. Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *    3. Neither the names of the authors nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.googlecode.syper.javasyp.formulae;

import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;

/**
 * @author kevin
 * @since Jan 30, 2010
 *
 */
public final class Local implements Name {

	public static final String FIELD_META_TYPE = "Field";

	public static final String ARRAY_LENGTH_FIELD_NAME = "length-";

	/**
	 * Field must be non-{@code null}; otherwise use
	 * {@link #createFieldVar(String, ITypeBinding)}.
	 * @param field
	 * @return
	 */
	public static Local createFieldVar(IVariableBinding field) {
		ITypeBinding type = field.getType();
		String id;
		if (field.getDeclaringClass() == null && "length".equals(field.getName()))
			// normalize array.length
			id = ARRAY_LENGTH_FIELD_NAME;
		else
			id = field.getDeclaringClass().getBinaryName() + "." + field.getName();
		return new Local(id, type, FIELD_META_TYPE, field);
	}

	public static Local createFieldVar(String id, ITypeBinding fieldType) {
		return new Local(id, fieldType, FIELD_META_TYPE);
	}

	private final String id;
	private final ITypeBinding type;
	private final String metaType;
	private final IVariableBinding binding;

	public Local(IVariableBinding var) {
		this(var.getName(), var.getType(), null, var);
	}

	public Local(IVariableBinding binding, int incarnation) {
		this(binding.getName() + (incarnation == 0 ? "" : incarnation),
				binding.getType(), null, binding);
	}

	public Local(String id, ITypeBinding type) {
		this(id, type, null);
	}

	public Local(String id, ITypeBinding type, String metaType) {
		this(id, type, metaType, null);
	}

	private Local(String id, ITypeBinding type, String metaType,
			IVariableBinding binding) {
		if (id == null)
			throw new IllegalArgumentException("id is null");
		if (type == null)
			throw new IllegalArgumentException("type is null");
		this.id = id;
		this.type = type;
		this.metaType = metaType;
		this.binding = binding;
	}

	public String getId() {
		return id;
	}

	public boolean isMeta() {
		return metaType != null;
	}

	public String getMetaType() {
		return metaType;
	}

	/**
	 * Binding of this variable provided on a best-effort basis.
	 * @return
	 */
	public IVariableBinding getBinding() {
		return binding;
	}

	/**
	 * This implementation should never return {@code null}.
	 * {@inheritDoc}
	 * @return {@inheritDoc}
	 */
	@Override
	public ITypeBinding getType() {
		return type;
	}

	@Override
	public <T> T dispatch(FormulaVisitor<T> visitor) {
		return visitor.visit(this);
	}

	@Override
	public boolean isBoolean() {
		return ! isMeta() && TypingUtil.isBoolean(type);
	}

	@Override
	public String toString() {
		return metaType == null ? id : id + "[" + metaType + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result
				+ ((metaType == null) ? 0 : metaType.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Local other = (Local) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (metaType == null) {
			if (other.metaType != null)
				return false;
		} else if (!metaType.equals(other.metaType))
			return false;
		return true;
	}
}
